import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/modules/application/education/quiz/widget.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/education/me_demand_research/me_demand_research_detail/findings_text/findings_text_controller.dart';


const _bgBlue = Color(0xFF5C6DFF);

///调研结果 （调研测试）
class FindingsTextPage extends StatelessWidget {
  FindingsTextPage({super.key});

  final controller = Get.put(FindingsTextController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Stack(
            children: [
              Column(
                children: [
                  Container(
                    color: _bgBlue,
                    height: 95.w,
                  ),
                  Expanded(
                    child: Container(
                      color: AppColors.Pure_gray_bg,
                    ),
                  ),
                ],
              ),
              _buildBody(),
            ],
          ),
        );
      },
    );
  }

  Widget _buildBody() {
    return ListView(
      padding: EdgeInsets.all(17.w),
      children: [
        _buildCard(),
        AppGaps.vGap10,
        _geBottomView(),
        AppGaps.vGap20,
      ],
    );
  }

  /// 主要部分 卡片
  Widget _buildCard() {
    return CustomTitleContentView(
      title: _buildCardTop(),
      content: _buildCardBottom(),
    );
  }

  /// 卡片的上部分
  Widget _buildCardTop() {
    final children = <Widget>[];
    final keys = controller.data?.summary?.keys.toList() ?? [];
    for (int idx = 0; idx < keys.length; idx++) {
      children.add(
        _buildCardTopCount(
          keys[idx],
          controller.data?.summary?[keys[idx]] ?? 0,
          idx == 0
              ? Alignment.centerLeft
              : idx == keys.length - 1
                  ? Alignment.centerRight
                  : Alignment.center,
        ),
      );
    }
    return Column(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 13.w, vertical: 7.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: children,
          ),
        ),
        AppGaps.vGap10,
        CustomCard.builtinDivider,
        AppGaps.vGap10,
        _getDetailsView(),
      ],
    );
  }

  Widget _getDetailsView() {
    return Container(
      // padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 20.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(6.w),
          topLeft: Radius.circular(6.w),
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              // Container(
              //   color: Colors.red,
              //   width: 13.94.w,
              //   height: 13.94.w,
              // ),
              SvgPicture.asset(
                AppImages.icon_findings_result_select,
              ),
              AppGaps.hGap6,
              Text(
                '已选择专题',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: const Color(0xFF262F38),
                  overflow: TextOverflow.ellipsis,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
          AppGaps.vGap6,
          Wrap(
            // spacing: 47.w, // 主轴(水平)方向间距
            // runSpacing: 12.w, // 纵轴（垂直）方向间距
            children: controller.list
                .map(
                  (e) => Text(
                    e,
                    style: TextStyle(
                      fontSize: 14.sp,
                      color: const Color(0xFF495061),
                      overflow: TextOverflow.ellipsis,
                      fontWeight: FontWeight.w400,
                    ),
                  ),
                )
                .toList(),
          ),
          AppGaps.vGap6,
        ],
      ),
    );
  }

  /// 卡片 上部分 的题目计数
  Widget _buildCardTopCount(String type, dynamic count, Alignment align) {
    return Container(
      width: 47.w,
      alignment: align,
      child: Column(
        children: [
          Text(
            count.toString(),
            style: TextStyle(
              //fontFamily: 'YouSheBiaoTiHei-Bold',
              fontSize: 16.w,
              color: _bgBlue,
            ),
          ),
          Text(
            type,
            style: TextStyle(fontSize: 11.w, color: const Color(0xFF495061)),
          )
        ],
      ),
    );
  }

  /// 卡片的下部分
  Widget _buildCardBottom() {
    return Container(
      padding: EdgeInsets.all(13.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                '答题卡',
                style: TextStyle(fontSize: 18.w, fontWeight: FontWeight.w500),
              ),
              QuizWidgets.statusIndicator,
            ],
          ),
          AppGaps.vGap16,
          _buildCardBottomQuizSummary(),
        ],
      ),
    );
  }

  Widget _buildCardBottomQuizSummary() {
    return SizedBox(
      height: 56.w * ((controller.quizConfig.topics.length / 5).ceil() + 1),
      width: 343.w,
      child: GridView(
        physics: const NeverScrollableScrollPhysics(),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 5),
        children: [
          for (int idx = 0; idx < controller.quizConfig.topics.length; idx++) QuizWidgets.topicDotIndicatorNoClick(controller.quizConfig.topics[idx], idx),
        ],
      ),
    );
  }

  Widget _geBottomView() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 13.w, vertical: 7.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          bottomRight: Radius.circular(6.w),
          bottomLeft: Radius.circular(6.w),
        ),
      ),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              SvgPicture.asset(
                AppImages.icon_findings_recommendation,
              ),
              AppGaps.hGap6,
              Text(
                '建议',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: const Color(0xFF262F38),
                  overflow: TextOverflow.ellipsis,
                  fontWeight: FontWeight.w400,
                ),
              ),
            ],
          ),
          AppGaps.vGap6,
          Align(
            alignment: Alignment.topLeft,
            child: Text(
              '${controller.recommendation}',
              style: TextStyle(
                fontSize: 14.sp,
                color: const Color(0xFF262F38),
                fontWeight: FontWeight.w400,
              ),
            ),
          )
        ],
      ),
    );
  }
}
